
// Copyright (c) 2002 HTHK
package com.ffpc.util;

import java.io.*;

/**
 * A Class class.
 * <P>
 * @author HTHK
 */
public class TextFileEditor
{
  private PrintWriter pw;
  private String path;
  private String filename;
  private File file;
  private StringBuffer content;

  public TextFileEditor()
  {
  }

  public TextFileEditor(String pathname,String filename)
  {
    this.setPath(pathname);
    this.setFilename(filename);
  }

  public final void openFile()
  {
    try
    {
      this.setFile(this.getFilename());
      content = load();
      append(content.toString());
    }
    catch (IOException e)
    {
      System.out.println(e.getMessage());
    }
  }

  private StringBuffer load()
    throws IOException
  {
      StringBuffer context = new StringBuffer();
      if (getFile().canRead())
      {
        InputStreamReader in = this.getInputStreamReader();
        char[] buffer = new char[1024];
        int length = 0;
        while ((length = in.read(buffer,0,buffer.length))!= -1)
        {
            context = context.append(buffer,0,length);
        }
        in.close();
      }
      return context;
  }

  public final void append(String s)
  {
      content = content.append(s);
  }

  public final void appendln(String s)
  {
      content = content.append(s + "\n");
  }

  public final String read()
    throws IOException
  {
    return load().toString();
  }

  public final void delete()
  {
    if (this.getFile().canRead() && this.getFile().canWrite())
       this.getFile().delete();
    else
        System.out.println("This file can't be deleted!");
  }

  public void save()
    throws IOException
  {
    pw = new PrintWriter (new FileOutputStream(this.getFile()));
    pw.print(content.toString());
    pw.flush();
    this.close();
  }

  public TextFileEditor saveAs(String pathname, String filename)
    throws IOException
  {
    try
    {
      TextFileEditor newFile = new TextFileEditor(pathname, filename);
      newFile.openFile();
      newFile.append(this.read());
      newFile.save();
      newFile.close();
      return newFile;
    }
    catch (IOException e)
    {
      System.out.println(e.getMessage());
      throw new IOException(e.getMessage());
    }
  }

  private InputStreamReader getInputStreamReader()
    throws FileNotFoundException
  {
    return new InputStreamReader (new FileInputStream(this.getFile()));
  }

  public void setPath(String path)
  {
    this.path = path;
  }

  public String getPath()
  {
    return this.path;
  }

  public void setFilename(String filename)
  {
    this.filename = filename;
  }

  public String getFilename()
  {
    return this.filename;
  }

  private void setFile(String filename)
    throws IOException
  {
    this.file = new File(this.getPath() + filename);
    if (!file.canRead())
      if (file.createNewFile())
        System.out.println("New file - "+filename+" created.");
  }

  private File getFile()
  {
    return this.file;
  }

  public void close()
  {
    try
    {
      if (pw!=null)
        pw.close();
    }
    catch (Exception e)
    {
      System.out.println(e.toString());
    }
  }
}

